DemonBar_Version = "0.1a";

--------------
--  locals  --
--------------

local L	= AceLibrary("AceLocale-2.0"):new("DemonBar")

-------------
-- globals --
-------------


----------------
--  defaults  --
----------------

local globalDefaults = {
	barPosition = {
		x = 100,
		y = -100,
	},
	locked = false,
}

	local options = {
		type = "group",
		args = {
			reset = {name = L["Reset"], desc = L["Return settings to their defaults."], type = "execute", func = "ResetDefaults"},},
	}
	


------------
--  init  --
------------

DemonBar = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0", "AceDB-2.0", "AceConsole-2.0", "AceHook-2.0")

function DemonBar:OnInitialize()
	self:RegisterDB("DemonBarOptions");
	self:RegisterDefaults('account', globalDefaults)
	self:RegisterChatCommand({'/demonbar'}, options)
end

function DemonBar:OnEnable()
	DemonBar:CreateFrames()
	DemonBar:OnTextUpdate()
	self.frames.main:Show()
end

function DemonBar:OnDisable()
end


-------------
--  hooks  --
-------------


-----------------
--  functions  --
-----------------
local GetItemSubtype
do
	local version = GetBuildInfo()
	if string.find(version, "^2%.") then
		-- 2.0.0
		function GetItemSubtype(num)
			local _,_,_,_,_,_,subtype = GetItemInfo(num)
			return subtype
		end
	else
		function GetItemSubtype(num)
			local _,_,_,_,_,subtype = GetItemInfo(num)
			return subtype
		end
	end
end

function DemonBar:OnTextUpdate()
	local current = 0
	local total = 0

	for i = 0, 4 do
		if i >= 1 then
			local link = GetInventoryItemLink("player", ContainerIDToInventoryID(i))
			if link ~= nil then
				local itemId = tonumber(string.gsub(link, "|cff%x%x%x%x%x%x|Hitem:(%d+):%d+:%d+:%d+|h.*", "%1") or 0)
				local subtype = GetItemSubtype(itemId)
				if subtype == L["Soul Bag"] then
					local size = GetContainerNumSlots(i)
					if size ~= nil and size > 0 then
						total = total + size
					end
				end
			end
		end
		for j = 1, GetContainerNumSlots(i) do
			local link = GetContainerItemLink(i, j)
			if link ~= nil and string.find(link, "|cff%x%x%x%x%x%x|Hitem:6265:%d+:%d+:%d+|h") then
				current = current + 1
			end
		end
	end
	if current > total then
		total = current
	end
	if total < 4 then
		total = 4
	end
	
	--DemonBar_ShardCount_OverlayText:SetText(current)
	self.frames.shardtext:SetText(current)
end

function DemonBar:CreateFrames()
	self.frames = {}
	self.frames.main = CreateFrame("Frame", nil, UIParent)
	self.frames.main.owner = self
	self.frames.main:Hide()
	self.frames.main:EnableMouse(true)
	self.frames.main:SetMovable(true)
	self.frames.main:SetWidth(1)
	self.frames.main:SetHeight(1)
	self.frames.main:SetPoint("TOPLEFT", UIParent, "TOPLEFT", self.db.account.barPosition.x, self.db.account.barPosition.y)
	
	self.frames.shard = CreateFrame( "Button", nil, self.frames.main )
	self.frames.shard.owner = self
	self.frames.shard:SetWidth(32)
	self.frames.shard:SetHeight(32)
	self.frames.shard:SetPoint("CENTER", self.frames.main, "CENTER" )
	self.frames.shard:RegisterForDrag("LeftButton")
	self.frames.shard:SetScript("OnDragStart", function() this.owner:StartMoving() end )
	self.frames.shard:SetScript("OnDragStop", function() this.owner:StopMoving() end )
	self.frames.shard:SetNormalTexture("Interface\\Icons\\INV_Misc_Gem_Amethyst_02")
	
	-- Text inside the counter		
	self.frames.shardtext = self.frames.shard:CreateFontString(nil, "OVERLAY")
	self.frames.shardtext.owner = self
	self.frames.shardtext:SetFontObject(GameFontNormalSmall)
	self.frames.shardtext:ClearAllPoints()
	self.frames.shardtext:SetTextColor(1, 1, 1, 1)
	self.frames.shardtext:SetWidth(32)
	self.frames.shardtext:SetHeight(32)
	self.frames.shardtext:SetPoint("TOPLEFT", self.frames.shard, "TOPLEFT")
	self.frames.shardtext:SetJustifyH("RIGHT")
	self.frames.shardtext:SetJustifyV("BOTTOM")
	self.frames.shardtext:SetText("MIDDLE")
end

function DemonBar:StartMoving()
	print "dragging"
	if not self.db.account.locked then
		self.frames.main:StartMoving()
	end
end

function DemonBar:StopMoving()
	self.frames.main:StopMovingOrSizing()
	local _,_,_,x,y = self.frames.main:GetPoint("CENTER")
	self.db.account.barPosition.x = x
	self.db.account.barPosition.y = y
end

function DemonBar:ResetDefaults()
	self:ResetDB("account")
	DemonBar:CreateFrames()
end